<template>
  <div class="mainRight">
    <el-row>
      <el-col :span="24">
        <el-button
          @click="(title = '新增配置'), (dialogVisible = true)"
          icon="el-icon-plus"
          size="mini"
          plain
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table :data="list" v-loading="loading" style="width: 100%">
      <el-table-column prop="uploadflag" label="标记" align="center">
      </el-table-column>
      <el-table-column prop="uploadremark" label="说明" align="center">
      </el-table-column>
      <el-table-column prop="uploadforld" label="文件夹名称" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button @click="editUpload(row)" type="text">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
		<pagination
			v-show="ruleForm.total > 0"
			:total="ruleForm.total"
			:page.sync="ruleForm.pageNum"
			:limit.sync="ruleForm.pageSize"
			@pagination="() => {}"
		/>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" @close="cancelForm('ruleForm')">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="120px"
        class="ruleForm"
      >
        <el-form-item label="上传标记" prop="uploadflag">
          <el-input
            v-model="ruleForm.uploadflag"
            placeholder="请填写上传标记"
          ></el-input>
        </el-form-item>
        <el-form-item label="上传说明" prop="uploadremark">
          <el-input
            v-model="ruleForm.uploadremark"
            placeholder="请填写上传说明"
          ></el-input>
        </el-form-item>
        <el-form-item label="上传文件夹名称" prop="uploadforld">
          <el-input
            v-model="ruleForm.uploadforld"
            placeholder="请填写上传文件夹名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelForm('ruleForm')">取 消</el-button>
        <el-button
          :disabled="btnLoading"
          type="primary"
          @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getUploadList, addUpload, editUpload } from "@/api/setting";
export default {
  data() {
    return {
      title: "",
      btnLoading: false,
      loading: true,
      dialogVisible: false,
      queryParams: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      list: [], //
      ruleForm: {
        id: null,
        uploadflag: "",
        uploadforld: "",
        uploadremark: "",
      },
      rules: {
        uploadflag: [
          { required: true, message: "请填写上传标记", trigger: "blur" },
        ],
        uploadforld: [
          { required: true, message: "请填写上传文件夹名称", trigger: "blur" },
        ],
        uploadremark: [
          { required: true, message: "请填写上传说明", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    editUpload(item) {
      this.title = "编辑配置";
      this.$nextTick(() => {
        let { uploadflag, uploadforld, uploadremark, id } = item;
        this.ruleForm = { uploadflag, uploadforld, uploadremark, id };
      });
      this.dialogVisible = true;
    },
    //切换分页
    handleCurrentChange(val) {
      this.loading = true;
      this.queryParams.pageNum = val;
      this.getList();
    },
    //切换总条数
    handleSizeChange(val) {
      this.loading = true;
      this.queryParams.pageSize = val;
      this.getList();
    },
    cancelForm(ruleForm) {
      this.dialogVisible = false;
      this.resetForm(ruleForm);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          let api = this.ruleForm.id
            ? editUpload(this.ruleForm)
            : addUpload(this.ruleForm);

          api
            .then((res) => {
              this.loading = true;
              this.btnLoading = false;
              this.cancelForm(formName);
              this.$modal.msgSuccess(res.msg);
              this.getList();
            })
            .catch(() => {
              this.btnLoading = false;
            });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    getList() {
      getUploadList(this.queryParams).then((res) => {
        this.loading = false;
        this.list = res.data.list;
        this.queryParams.total = res.data.total;
      });
    },
  },
  created() {
    this.getList();
  },
};
</script>
